<!-- 属性筛选区域 -->
<template>
  <!-- 筛选区 -->
  <div v-if="!filterLoading && filterData" class="sub-filter">
    <!-- 品牌单独渲染 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          v-for="brand in filterData.brands"
          :key="brand.id"
          :class="{ active: filterData.selectedBrand === brand.id }"
          @click="changeBrand(brand)"
        >{{ brand.name }}</a>
      </div>
    </div>
    <!-- 属性渲染 -->
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">{{ p.name }}：</div>
      <div class="body">
        <a
          :class="{ active: p.selectedProp === attr.id }"
          v-for="attr in p.properties"
          :key="attr.id"
          @click="changeAttr(p, attr)"
        >{{ attr.name }}</a>
      </div>
    </div>
  </div>

  <div v-else class="sub-filter">
    <Skeleton class="item" width="800px" height="30px" bg-color="#EAEAEA" />
    <Skeleton class="item" width="800px" height="30px" bg-color="#EAEAEA" />
    <Skeleton class="item" width="600px" height="20px" bg-color="#EAEAEA" />
    <Skeleton class="item" width="600px" height="20px" bg-color="#EAEAEA" />
    <Skeleton class="item" width="600px" height="18px" bg-color="#EAEAEA" />
  </div>
</template>
<script>
import { findSubCategoryFilter } from "@/api/category.js"
import { watch, ref } from "vue"
import { useRoute } from "vue-router"
export default {
  name: 'SubFilter',
  setup(props, { emit }) {
    // 1. 获取数据
    // 2. 数据中需要全部选中，需要预览将来点击激活功能。默认选中全部
    // 3. 完成渲染
    const filterData = ref({})
    const route = useRoute()
    const filterLoading = ref(false)
    // 4. 分类发生变化的时候需要重新获取筛选数据，需要使用侦听器
    watch(() => (route.params.id), async (newVal) => {
      // 当你从二级分类去顶级分类也会拿到ID，不能去加载数据因为它不是二级分类的ID
      if (newVal && `/category/sub/${newVal}` === route.path) {
        filterLoading.value = true
        const { result: ret } = await findSubCategoryFilter(newVal)
        ret.selectedBrand = null
        ret.brands.unshift({ id: null, name: '全部' })
        ret.saleProperties.forEach(item => {
          item.selectedProp = null
          item.properties.unshift({ id: null, name: '全部' })
        })
        filterData.value = ret
        filterLoading.value = false
      }
    }, { immediate: true })

    // 获取筛选数据
    const getFilterParams = () => {
      const filterParams = {}
      const attrs = []
      filterParams.brandId = filterData.value.selectedBrand
      // 查看每个属性的选中状态
      filterData.value.saleProperties.forEach(e => {
        //获取选中的属性
        const attr = e.properties.find(attr => attr.id === e.selectedProp)
        if (attr && attr.id != null) {
          attrs.push({ groupName: e.name, propertyName: attr.name })
        }
      })
      if (attrs.length) filterParams.attrs = attrs
      return filterParams
    }

    // 选择品牌触发刷新
    const changeBrand = brand => {
      filterData.value.selectedBrand = brand.id
      emit('filter-change', getFilterParams())

    }
    // 选择属性触发刷新
    const changeAttr = (p, attr) => {
      if (attr.id === p.selectedProp) return
      p.selectedProp = attr.id
      emit('filter-change', getFilterParams())
    }

    return {
      filterData,
      filterLoading,
      getFilterParams,
      changeBrand,
      changeAttr
    }
  }
}
</script>


<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .skeleton {
    margin: 10px 0;
  }
}
</style>